layui.define(['table', 'form'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form;

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/agent/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
       {field: 'userId', title: '用户ID'}
      ,{field: 'name', title: '用户名称',templet:function (record){
          return record.user.name;
              }}
      ,{field: 'phone', title: '用户手机号',templet:function (record){
                  return record.user.phone;
              }}
      ,{field: 'nickname', title: '昵称',templet:function (record){
                  return record.user.nickname;
              }}
	  ,{field: 'typeNames', title: '代理类型'}
      ,{field: 'levelName', title: '代理等级'}
      ,{field: 'createDate', title: '创建时间'}
      ,{title: '操作', width: 240, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
      ,page: {curr: 1}
    });
  });
  
  //事件
  var active = {
      add:function (obj){
        layer.open({
            type: 2
            , title: '新增代理'
            , content: layui.setter.webRoot + '/agent/add.html'
            , maxmin: true
            , area: ['500px', '550px']
            ,btn: ['确定', '取消']
            , yes: function(index, layero){
                var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submitID = 'LAY-form-submit'
                    ,submit = layero.find('iframe').contents().find('#'+ submitID);
                iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                    var field = data.field;
                    $.ajax({
                        type: "post",
                        url: layui.setter.webRoot + '/agent/add.do',
                        data:field,
                        dataType:"json",
                        success:function(res){
                            if(res.code == 200){
                                table.reload('LAY-table');
                                layer.close(index);
                            }else{
                                layer.msg(res.msg,{icon: 2});
                            }
                        }
                    });
                });
                submit.trigger('click');
            }
        })
      }
      ,info:function(obj){
          layer.open({
              type: 2
              ,title: obj.data.name
              ,content: layui.setter.webRoot + '/agent/info.html?userId='+obj.data.userId
              ,maxmin: true
              ,area: ['90%', '90%']
              ,btn: ['取消']
          });
      }
      ,walletInfo:function(obj){
          layer.open({
              type: 2
              ,title: obj.data.name
              ,content: layui.setter.webRoot + '/user/wallet/info.html?id='+obj.data.userId
              ,maxmin: true
              ,area: ['90%', '90%']
              ,btn: ['取消']
          });
      }
	  ,placeList:function(obj){
	        layer.open({
	            type: 2
	            ,title: obj.data.name
	            ,content: layui.setter.webRoot + '/agent/place/list.html?userId='+obj.data.userId
	            ,maxmin: true
	            ,area: ['90%', '90%']
	            ,btn: ['取消']
	        });
	  }
      ,del:function(obj){
          layer.confirm('确定要删除该用户信息吗？', function(index){
                $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/agent/del.do',
                  data:{userId:obj.data.userId},
                  dataType:"json",
                  success:function(res){
                    if(res.code == 200){
                        obj.del();
                    }else{
                        layer.msg(res.msg,{icon: 5});
                    }
                  }
                });
                layer.close(index);
	      });
      }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('agent_list', {})
});